package com.agent.crm.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.agent.crm.base.BaseActivity;
import com.agent.crm.config.AppConfig;
import com.agent.crm.service.InitDataService;

public class WelcomeActivity extends BaseActivity {
	
	private ProgressDialog pdDialog; 

	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return R.layout.welcome;
	}

	@Override
	protected void initViewCmp() {
		// TODO Auto-generated method stub
		SharedPreferences preferences =getSharedPreferences("initData",Context.MODE_PRIVATE);
		boolean initContacts= preferences.getBoolean("initContacts", false);
		if(!initContacts){
			pdDialog =new ProgressDialog(this);
			pdDialog.setMessage("首次使用正在初始化数据,可能需要一定时间，请稍候...");
			pdDialog.setCanceledOnTouchOutside(false);
			pdDialog.setCancelable(false);
			pdDialog.show();
			AppConfig.loadingFromMobileContacts=true;
			startService(new Intent(WelcomeActivity.this,InitDataService.class));
		}
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void initAsycnData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		/**
         * 切换到主界面（淡入淡出效果）
         */
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (AppConfig.loadingFromMobileContacts) {
					SharedPreferences preferences =getSharedPreferences("initData",Context.MODE_PRIVATE);
					boolean initContacts= preferences.getBoolean("initContacts", false);
					if(initContacts){
						AppConfig.loadingFromMobileContacts =false;
					}
				}
				handler.sendEmptyMessage(0);
			}
		}).start();
        
	}
	
	Handler handler =new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			new CountDownTimer(5000,1000) {

	        	@Override
	        	public void onTick(long millisUntilFinished) {}
	        	
	        	@Override
	        	public void onFinish() {
	        		SharedPreferences preferences =getSharedPreferences("login_data", Context.MODE_PRIVATE);
	    			boolean isAutoLogin=preferences.getBoolean("isAutoLogin",false);
	    			Intent intent = new Intent();
	    			if(isAutoLogin){
	    				String isLogin =preferences.getString("isLogin", "false");
	    				if(isLogin.equalsIgnoreCase("true")){
	    	        		intent.setClass(WelcomeActivity.this, MainActivity.class);
	    				}else{
	    					intent.setClass(WelcomeActivity.this, LoginActivity.class);
	    				}
	    			}else{
	    				intent.setClass(WelcomeActivity.this, LoginActivity.class);
	    			}
	    			startActivity(intent);
	    			if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR)
	        			WelcomeActivity.this.overridePendingTransition(R.anim.apha_out, R.anim.apha_in);
	        		if(null!=pdDialog&&pdDialog.isShowing())
	        			pdDialog.dismiss();
	        		finish();
	        	}
	        }.start();
		}
		
	};

	@Override
	protected View initCustomLayout() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void changeDefaultSetting() {
		// TODO Auto-generated method stub
		
	}

	
}